package exp;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import tools.FileReaderEnhance;
import tools.ObjectTools;
import tools.webkit.Webpage;

/**
 * Calculation of propotions for given websites
 * @author Lanjun
 *
 */
public class EXP4 {
	static String path = "data/objs/urlContent2/";
	
	public static void main(String[] args) {
		try {
			System.setOut(new PrintStream(new File("./logs/EXP4.out.log")));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		System.out.println("\t" + "edition.cnn.com\twww.usatoday.com\tnews.xinhuanet.com\twww.bbc.co.uk");
		String [] topicList = FileReaderEnhance.readToString("data/objs/topiclist.dat", "UTF-8").split("\n");
		for (String topic : topicList) {
			HashMap<String, Integer> urlCount = new HashMap<>();
			for (File file : (new File(path)).listFiles()) {
				if (!file.getName().startsWith(topic)) {
					continue;
				}
				@SuppressWarnings("unchecked")
				List<Webpage> w = (ArrayList<Webpage>)ObjectTools.readFromFile(file);
				for (Webpage page : w) {
					try {
						URL url = new URL(page.realURL);
						String host = url.getHost();
						if (urlCount.containsKey(host)) {
							urlCount.put(host, urlCount.get(host) + 1);
						} else {
							urlCount.put(host, 1);
						}
					} catch (MalformedURLException e) {
						e.printStackTrace();
					}
				}
			}
			System.out.print(topic);
			System.out.print("\t" + urlCount.get("edition.cnn.com"));
			System.out.print("\t" + urlCount.get("www.usatoday.com"));
			System.out.print("\t" + urlCount.get("news.xinhuanet.com"));
			System.out.println("\t" + urlCount.get("www.bbc.co.uk"));
//			for (String key : urlCount.keySet()) {
//				System.out.println(key + "\t" + urlCount.get(key));
//			}
		}
	}
}
